package com.opesoft.pf.page;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;

/**
 * 进度管理器
 * 
 * @author Larlf
 * 
 */
public class ProcessManager
{
	static public long timeOut = 12 * 60 * 60 * 1000; //默认12个小时超时
	static public long clearTime = 4 * 60 * 60 * 1000; //4个小时进行一次清理

	static int _id = 0; //用于累计生成新的ID

	static Map processes = new HashMap(); //存放所有的ProcessRate对象

	static
	{
		Timer timer = new Timer();
		timer.schedule(new ProcessRateTimerTask(), timeOut);
	}

	static public int getId()
	{
		int id = ProcessManager._id++;

		processes.put("p" + id, new ProcessRate());

		return id;
	}

	static public void setCount(int id, int count)
	{
		Object obj = processes.get("p" + id);
		if(obj != null)
		{
			((ProcessRate) obj).setCount(count);
		}
	}

	static public void setRate(int id, int rate)
	{
		Object obj = processes.get("p" + id);
		if(obj != null)
		{
			((ProcessRate) obj).setRate(rate);
		}
	}

	static public void finish(int id)
	{
		processes.remove("p" + id);
	}

	static public ProcessRate getProcessRate(int id)
	{
		Object obj = processes.get("p" + id);
		if(obj != null)
			return (ProcessRate) obj;
		else
			return ProcessRate.NULL;
	}

	/**
	 * 取得当前正在进行的进程数
	 * 
	 * @return
	 */
	static public int getProcessesCount()
	{
		return processes.size();
	}
}
